<template>
  <div class="app-container">
    <!--  搜索框  -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="用户名称">
        <el-input v-model="queryParams.username"
                  placeholder="请输入用户名称"
                  clearable
                  size="small"
                  style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="queryParams.phone"
                  placeholder="请输入手机号码"
                  size="small"
                  style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status"
                   placeholder="用户状态"
                   size="small"
                   style="width: 240px"
        >
          <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--  操作按钮  -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            icon="el-icon-plus"
            type="primary"
            plain
            size="mini"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            icon="el-icon-edit"
            type="success"
            plain
            size="mini"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            icon="el-icon-delete"
            type="danger"
            plain
            size="mini"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            icon="el-icon-download"
            type="warning"
            plain
            size="mini"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--  用户列表  -->
    <el-table :data="userList">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="用户编号" align="center" key="userId" width="120" prop="userId"
                       v-if="columns[0].visible"/>
      <el-table-column label="用户名称" align="center" key="username" width="150" prop="username"
                       v-if="columns[1].visible" :show-overflow-tooltip="true"/>
      <el-table-column label="用户昵称" align="center" key="nickname" width="120" prop="nickname"
                       v-if="columns[2].visible" :show-overflow-tooltip="true"/>
      <el-table-column label="手机号码" align="center" key="phonenumber" prop="phone" v-if="columns[4].visible"
                       width="120"/>
      <el-table-column label="状态" width="100" align="center" key="status" v-if="columns[5].visible">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.status"
              active-value="0"
              inactive-value="1"
              @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180" align="center" prop="createTime"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope" v-if="scope.row.userId !== 1">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
          >删除
          </el-button>
          <el-dropdown
              size="mini"
          >
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item icon="el-icon-key">重置密码</el-dropdown-item>
              <el-dropdown-item icon="el-icon-circle-check">分配角色</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    ></pagination>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog  :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickname">
              <el-input v-model="form.nickname" placeholder="请输入用户昵称" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号码" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户性别">
              <el-select v-model="form.gender" placeholder="请选择性别">
                <el-option
                    v-for="dict in dict.type.sys_user_sex"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.userId === undefined" label="用户名称" prop="userName">
              <el-input v-model="form.username" placeholder="请输入用户名称" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.userId === undefined" label="用户密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入用户密码" type="password" maxlength="20" show-password/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                    v-for="dict in dict.type.sys_normal_disable"
                    :key="dict.value"
                    :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="角色">
              <el-select v-model="form.roleIds" multiple placeholder="请选择角色">
                <el-option
                    v-for="item in roleOptions"
                    :key="item.roleId"
                    :label="item.roleName"
                    :value="item.roleId"
                    :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
export default {
  name: 'user',
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: true,
      // 用户表格数据
      userList: [
        {
          createBy: "admin",
          createTime: "2023-04-23 16:11:38",
          updateBy: null,
          updateTime: null,
          remark: "管理员",
          userId: 1,
          deptId: 103,
          username: "admin",
          nickname: "若依",
          email: "ry@163.com",
          phone: "15888888888",
          gender: "1",
          avatar: "",
          password: null,
          status: "0",
          delFlag: "0",
          loginIp: "117.81.137.193",
          loginDate: "2023-07-01T10:58:02.000+08:00",
          roles: [],
          roleIds: null,
          postIds: null,
          roleId: null,
          admin: true
        },
        {
          createBy: "admin",
          createTime: "2023-04-23 16:11:38",
          updateBy: null,
          updateTime: null,
          remark: "测试员",
          userId: 2,
          deptId: 105,
          username: "ry",
          nickname: "若依",
          email: "ry@163.com",
          phone: "15666666666",
          gender: "1",
          avatar: "",
          password: null,
          status: "0",
          delFlag: "0",
          loginIp: "222.88.237.131",
          loginDate: "2023-07-01T10:58:02.000+08:00",
          roles: [],
          roleIds: null,
          postIds: null,
          roleId: null,
          admin: true
        },
      ],
      // 日期范围
      dateRange: [],
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: undefined,
        phone: undefined,
        status: undefined,
      },
      // 列信息
      columns: [
        {key: 0, label: `用户编号`, visible: true},
        {key: 1, label: `用户名称`, visible: true},
        {key: 2, label: `用户昵称`, visible: true},
        {key: 3, label: `部门`, visible: true},
        {key: 4, label: `手机号码`, visible: true},
        {key: 5, label: `状态`, visible: true},
        {key: 6, label: `创建时间`, visible: true}
      ],
      // 表单校验
      rules: {
        username: [
          {required: true, message: "用户名称不能为空", trigger: "blur"},
          {min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur'}
        ],
        nickname: [
          {required: true, message: "用户昵称不能为空", trigger: "blur"}
        ],
        password: [
          {required: true, message: "用户密码不能为空", trigger: "blur"},
          {min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur'}
        ],
        email: [
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    }
  },
  methods: {
    // 查询用户列表
    getList() {
      this.loading = true;
    },
    // 搜索按钮操作
    handleQuery() {

    },
    // 重置按钮操作
    resetQuery() {

    },
    // 用户状态修改
    handleStatusChange() {

    },
    // 修改按钮操作
    handleUpdate() {

    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        username: undefined,
        nickname: undefined,
        password: undefined,
        phone: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: []
      };
      //this.resetForm("form");
    },
  }
}
</script>

<style scoped>

</style>
